define(function(require,exports,module){
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	
    module.exports = {
    	save: function(widget){
    		var endGrid = widget.get("endGrid");
        	var endGridData = endGrid.getData();
        	var totalGrid = widget.get("totalGrid");
        	var dat = totalGrid.getData();
        	var list = [];
        	if(isNaN(dat[0].receiptsTotal)){
    			Dialog.alert({
            		title:"提示",
            		content:"请填写实收金额合计(不能为空,且只能输入数字)！"
            	});
    			return false;
    		}
        	var paymentMethods = [];
    		if(dat[0].paymentMethod.length == 0){
    			Dialog.alert({
            		title:"提示",
            		content:"请填写结算方式！"
            	});
    			return false;
    		}else{
    			for(var i=0; i < dat[0].paymentMethod.length; i++){
    				paymentMethods[i] = dat[0].paymentMethod[i].key;
    			}
    		}
        	for(var i = 0;i<endGridData.length;i++){
        		list.push(endGridData[i].checkOutSettlement);
        	}
        	var view = {};
        	view.list = list;
        	view.balance = parseFloat(dat[0].receiptsTotal);
        	view.paymentMethods = paymentMethods;
        	view.description = dat[0].description;
        	var onCredit = widget.get("onCreditForm");
        	if(onCredit){
        		var onCreditData=onCredit.getData();
            	view.cImplement = onCreditData;
        	}
        	
        	for(var i in paymentMethods){
    			if(paymentMethods[i] == "OnCredit"){
    				view.cImplement.isSales = false;
    				break;
    			}
    		}
        	
        	
        	aw.ajax({
    			url:"api/checkin/checkoutsettlementsave",
    			data: aw.customParam(view),
    			type: "POST",
    			dataType:"json",
    			success:function(idata){
    				Dialog.alert({
						content: "保存成功！"
					});
    				widget.show([".J-grid"]).hide([".J-form",".J-endGrid",".J-total",".J-detailForm",".J-onCreditForm"]);
    				widget.get("subnav").hide(["return","save"]).show(["statementStatus","alliance","servicePoint","destination", "time"]);
        		    widget.get("grid").refresh();
    			}
    		});
    	}
    };
});
